# main.py
"""
MicroPython 协作开发指南 - 主程序文件
MicroPython Collaborative Development Guide - Main Program File

提供统一的硬件抽象接口
Main program file providing unified hardware abstraction interface
"""

from config import USE_MOCK_MODE, get_config
from typing import Union, Optional, Any

# 动态导入模块 | Dynamic module import
if USE_MOCK_MODE:
    print("🧪 Running in MOCK MODE - No hardware required")
    from gpio.mock_functions import (
        MockPin as Pin,
        set_pin_high_mock as set_pin_high_impl,
        read_pin_state_mock as read_pin_state_impl,
        toggle_pin_mock as toggle_pin_impl
    )
    from uart.mock_functions import (
        MockUART as UART,
        uart_send_mock as uart_send_impl,
        uart_receive_mock as uart_receive_impl,
        uart_send_receive_mock as uart_send_receive_impl
    )
else:
    print("🔧 Running in HARDWARE MODE - Real hardware required")
    try:
        from machine import Pin, UART
        from gpio.functions import (
            set_pin_high as set_pin_high_impl,
            read_pin_state as read_pin_state_impl,
            toggle_pin as toggle_pin_impl
        )
        from uart.functions import (
            uart_send as uart_send_impl,
            uart_receive as uart_receive_impl,
            uart_send_receive as uart_send_receive_impl
        )
    except ImportError as e:
        print(f"⚠️  Hardware modules not available: {e}")
        print("🔄 Falling back to MOCK MODE")
        # 回退到模拟模式 | Fallback to mock mode
        from gpio.mock_functions import (
            MockPin as Pin,
            set_pin_high_mock as set_pin_high_impl,
            read_pin_state_mock as read_pin_state_impl,
            toggle_pin_mock as toggle_pin_impl
        )
        from uart.mock_functions import (
            MockUART as UART,
            uart_send_mock as uart_send_impl,
            uart_receive_mock as uart_receive_impl,
            uart_send_receive_mock as uart_send_receive_impl
        )

# 统一接口函数 | Unified interface functions
def create_pin(pin_id: int, mode: str = 'output') -> Pin:
    """
    创建引脚对象 | Create pin object
    
    参数 Parameters:
    - pin_id (int): 引脚编号 | Pin number
    - mode (str): 引脚模式 | Pin mode
    
    返回值 Returns:
    - Pin: 引脚对象 | Pin object
    """
    if USE_MOCK_MODE:
        return Pin(pin_id, mode)
    else:
        # 转换模式参数 | Convert mode parameter
        mode_map = {'output': Pin.OUT, 'input': Pin.IN}
        return Pin(pin_id, mode_map.get(mode, Pin.OUT))

def create_uart(uart_id: int, **kwargs) -> UART:
    """
    创建UART对象 | Create UART object
    
    参数 Parameters:
    - uart_id (int): UART编号 | UART ID
    - **kwargs: 其他参数 | Other parameters
    
    返回值 Returns:
    - UART: UART对象 | UART object
    """
    if USE_MOCK_MODE:
        return UART(uart_id, **kwargs)
    else:
        baudrate = kwargs.get('baudrate', get_config('hardware.uart_baudrate'))
        return UART(uart_id, baudrate=baudrate)

# 导出统一接口 | Export unified interface
def set_pin_high(pin: Pin) -> None:
    """统一的引脚设置高电平接口 | Unified pin set high interface"""
    return set_pin_high_impl(pin)

def read_pin_state(pin: Pin) -> int:
    """统一的引脚状态读取接口 | Unified pin state read interface"""
    return read_pin_state_impl(pin)

def toggle_pin(pin: Pin) -> int:
    """统一的引脚切换接口 | Unified pin toggle interface"""
    return toggle_pin_impl(pin)

def uart_send(uart: UART, data: Union[str, bytes]) -> Optional[int]:
    """统一的UART发送接口 | Unified UART send interface"""
    return uart_send_impl(uart, data)

def uart_receive(uart: UART, timeout: int = 1000) -> Optional[bytes]:
    """统一的UART接收接口 | Unified UART receive interface"""
    return uart_receive_impl(uart, timeout)

def uart_send_receive(uart: UART, data: Union[str, bytes], timeout: int = 1000) -> Optional[bytes]:
    """统一的UART发送接收接口 | Unified UART send-receive interface"""
    return uart_send_receive_impl(uart, data, timeout)

if __name__ == "__main__":
    print("MicroPython 协作开发指南 - 主程序")
    print("MicroPython Collaborative Development Guide - Main Program")
    print(f"运行模式 | Running Mode: {'MOCK' if USE_MOCK_MODE else 'HARDWARE'}")
